define([ 'jquery', 'bootstrap', 'backend', 'table', 'form' ], function ($, undefined, Backend, Table, Form) {
	
	var Controller = {
		index: function () {
			// 初始化表格参数配置
			Table.api.init({
				extend: {
					index_url: 'user/withdrawal/index' + location.search,
					add_url: 'user/withdrawal/add',
					edit_url: 'user/withdrawal/edit',
					del_url: 'user/withdrawal/del',
					multi_url: 'user/withdrawal/multi',
					import_url: 'user/withdrawal/import',
					table: 'user_withdrawal'
				}
			});
			
			var table = $("#table");
			
			// 初始化表格
			table.bootstrapTable({
				url: $.fn.bootstrapTable.defaults.extend.index_url,
				pk: 'id',
				sortName: 'id',
				columns: [
					[
						{ checkbox: true },
						{ field: 'id', title: __('Id') },
						{ field: 'user.nickname', title: __('用户名称'), operate: 'LIKE' },
						{ field: 'cost', title: __('Cost'), operate: 'BETWEEN' },
						{
							field: 'type',
							title: __('Type'),
							searchList: { "0": __('Type 0'), "1": __('Type 1') },
							formatter: Table.api.formatter.normal
						},
						
						{
							field: 'type',
							title: __('打款信息'),
							operate: false,
							align: 'left',
							formatter: function (value, row, index) {
								if ( value==0 ) {
									return '<div class="cell">' +
										'<div class="display-flex" style="padding: 5px;"><span class="ellipsis-item">银行开户名：' + row.bank_name + '</span></div>' +
										'<div class="display-flex" style="padding: 5px;"><span class="ellipsis-item">银行卡号&nbsp;&nbsp;&nbsp;：' + row.bank_account + '</span></div>' +
										'<div class="display-flex" style="padding: 5px;"><span class="ellipsis-item">开户行&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;：' + row.bank_name + '</span></div>' +
										'</div>';
								} else {
									return '<div class="cell">' +
										'<div class="display-flex" style="padding: 5px;"><span class="ellipsis-item">支付宝用户名：' + row.ali_username + '</span></div>' +
										'<div class="display-flex" style="padding: 5px;"><span class="ellipsis-item">支付宝账号&nbsp;&nbsp;&nbsp;：' + row.ali_account + '</span></div>' +
										'</div>';
								}
							}
						},
						{
							field: 'status',
							title: __('Status'),
							searchList: { "0": __('Status 0'), "1": __('Status 1'), "2": __('Status 2') },
							formatter: Table.api.formatter.status
						},
						{
							field: 'createtime',
							title: __('Createtime'),
							operate: 'RANGE',
							addclass: 'datetimerange',
							autocomplete: false,
							formatter: Table.api.formatter.datetime
						},
						{
							field: 'operate',
							title: __('Operate'),
							table: table,
							formatter: function (value, row) {
								if ( row.status==0 ) {
									return '<div class="cell">' +
										'<span class="custom-table-operation-text custom-table-color-primary agree" data-id="' + row.id + '" style="color: #6E3DC8; cursor: pointer;">立即打款</span> ' +
										'<span class="custom-table-operation-text custom-table-color-refund" data-id="' + row.id + '" style="color: #ED5B56; cursor: pointer; margin-left: 10px;">拒绝</span> ' +
										'</div>';
								} else if ( row.status==2 ) {
									return '<div class="cell">' +
										'<span class="custom-table-operation-text custom-table-color-reason" data-reason="' + row.reason + '" style="color: #217dbb; cursor: pointer; margin-left: 10px;">查看拒绝理由</span> ' +
										'</div>';
								}
							}
						}
					]
				]
			});
			/*立即打款*/
			$(document).on("click", ".custom-table-color-primary.agree", function () {
				var id = $(this).data('id');
				layer.confirm('请确认已经进行提现汇款', {
					btn: [ '确定', '取消' ] //按钮
				}, function () {
					Fast.api.ajax('/root.php/user/withdrawal/sure/ids/' + id, function () {
						layer.msg('操作成功', { icon: 1 });
						$(".btn-refresh").trigger("click");
					})
				}, function () {
				
				});
			});
			/*拒绝退款*/
			$(document).on("click", ".custom-table-color-refund", function () {
				var id = $(this).data('id');
				
				layer.prompt({ title: '请输入拒绝理由', formType: 2 },
					function (pass, index) {
						Fast.api.ajax('/root.php/user/withdrawal/refund/ids/' + id + '/reason/' + pass, function () {
							layer.close(index);
							layer.msg('操作成功', { icon: 1 });
							$(".btn-refresh").trigger("click");
						})
					});
			});
			/*拒绝退款*/
			$(document).on("click", ".custom-table-color-reason", function () {
				var reason = $(this).data('reason');
				layer.alert(decodeURIComponent(reason));
			});
			// 为表格绑定事件
			Table.api.bindevent(table);
		},
		add: function () {
			Controller.api.bindevent();
		},
		edit: function () {
			Controller.api.bindevent();
		},
		api: {
			bindevent: function () {
				Form.api.bindevent($("form[role=form]"));
			}
		}
	};
	return Controller;
});